/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package app.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tuan Long
 */
public class DatabaseInteraction {

    public static Connection getConnection() {
        Connection cnn = null;
        try {
            String url = "jdbc:sqlserver://localhost\\express:1433;databaseName=ILoveShoppingCart";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try {
                cnn = DriverManager.getConnection(url, "sa", "123456");
            } catch (SQLException ex) {
                Logger.getLogger(DatabaseInteraction.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseInteraction.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return cnn;
        }
    }
    
    public static ResultSet getExcuteQuery(String query){
        try {
            Connection cnn = getConnection();
            CallableStatement cstm = cnn.prepareCall(query);
            return cstm.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseInteraction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
